-- =============================================
-- Create database template
-- =============================================
USE master
GO

-- Drop the database if it already exists
IF  EXISTS (SELECT name FROM sys.databases WHERE name = N'DAILY')
BEGIN	

	while(exists(select 1 from INFORMATION_SCHEMA.TABLE_CONSTRAINTS where CONSTRAINT_TYPE='FOREIGN KEY'))
	begin
		declare @sql nvarchar(2000)
		SELECT TOP 1 @sql=('ALTER TABLE ' + TABLE_SCHEMA + '.[' + TABLE_NAME
		+ '] DROP CONSTRAINT [' + CONSTRAINT_NAME + ']')
		FROM information_schema.table_constraints
		WHERE CONSTRAINT_TYPE = 'FOREIGN KEY'
		exec (@sql)
	end

	DROP DATABASE DAILY
END
GO

CREATE DATABASE DAILY
GO